/*
@最初版本开发日期: 2010年08月19日
@著作权所有: Lou Barnes ( http://www.loubarnes.com )

报告漏洞，意见或建议, 请联系 Lou Barnes(iua1024@gmail.com) 或登陆 http://www.loubarnes.com
*/


// 为新添加的节点分配临时ID
var g_iNewMenuID = 0;


$(function(){	
	updateOrderIcon();	
});

// 添加菜单
function addSubMenu(iMenuID)
{
	if(g_iNewMenuID==0)
	{
		for(var x in g_oMenuChain)
		{
			x = Number(x);
			if(g_iNewMenuID<x) g_iNewMenuID = x+1;
		}
	}
	
	g_iNewMenuID++;
	
	if(iMenuID==0)	// iMenuID为0 时, 没有指定父节点， 因些直接添加顶级菜单， 并在将该节点添加到链表的尾部
	{
		var str = '<tr id="row-'+g_iNewMenuID+'" onMouseOver="this.className=\'row-on\'" onMouseOut="this.className=\'row-off\'">'
		str += '<td></td>'
		str += '<td></td>'
		str += '<td>';
		str += '<input type="hidden" name="id[]" id="id-'+g_iNewMenuID+'" value="0" />';
		str += '<input type="hidden" name="parent_id[]" id="parent_id-'+g_iNewMenuID+'" value="0" />';
		str += '<input type="text" class="m_name" name="name[]" id="name-'+g_iNewMenuID+'" size="20" maxlength="120" />';
		str += '</td>';
		str += '<td align="center">';
		str += '<a href="javascript:;" onclick="javascript:setLink('+g_iNewMenuID+');" id="link-'+g_iNewMenuID+'" title="点击设置">未设置</a>';
		str += '<input type="hidden" class="m_controller" name="m_controller[]" id="controller-'+g_iNewMenuID+'" />';
		str += '<input type="hidden" name="m_task[]" id="task-'+g_iNewMenuID+'" />';
		str += '<input type="hidden" name="param[]" id="param-'+g_iNewMenuID+'" />';
		str += '<input type="hidden" name="link_to[]" id="link_to-'+g_iNewMenuID+'" />';
		str += '</td>';
		str += '<td align="center">';
		str += '<input type="text" name="url[]" id="url-'+g_iNewMenuID+'" size="20" maxlength="120" />';
		str += '</td>';
		str += '<td align="center">';
		str += '<a href="javascript:;" onclick="javascript:orderUp('+g_iNewMenuID+')" class="order-up-on" id="order-up-'+g_iNewMenuID+'"></a>';
		str += '</td>';
		str += '<td align="center">';
		str += '<a href="javascript:;" onclick="javascript:orderDown('+g_iNewMenuID+')" class="order-down-on" id="order-down-'+g_iNewMenuID+'"></a>';
		str += '</td>';		
		str += '<td align="center"><a href="javascript:;" onclick="javascript:deleteMenu('+g_iNewMenuID+')" class="delete"></a></td>';
		str += '</tr>';
		
		if(g_iMenuChainHead)
		{
			// 如果已有表头存在，则从表头开始遍历. 直到找到链表的结尾
			var oMenu = g_oMenuChain[g_iMenuChainHead];
			var oThisMenu = oMenu;
			while(oThisMenu.next_id!=0)
			{
				oThisMenu =  g_oMenuChain[oThisMenu.next_id];	// 链表后移
			}
			
			
			oThisMenu.next_id = g_iNewMenuID;
			g_oMenuChain[g_iNewMenuID] = {"pre_id":oThisMenu.id,"next_id":0,"id":g_iNewMenuID,"name":"","parent_id":0,"level":0};
			$("#row-"+oThisMenu.id).after(str);
		}
		else
		{
			g_iMenuChainHead = g_iNewMenuID;
			g_oMenuChain[g_iNewMenuID] = {"pre_id":0,"next_id":0,"id":g_iNewMenuID,"name":"","parent_id":0,"level":0};
			$("#row-list").append(str);
		}
		
		updateOrderIcon();
		return;
	}

	// 用户指定了在 iMenuID 添加子节点， 遍历链表中iMenuID之后的元素， 直到找出不是iMenuID节点子孙的节点。 在该节点之前添加即可
	var oMenu = g_oMenuChain[iMenuID];
	var oThisMenu = oMenu;
	
	if(oThisMenu.next_id>0)
	{
		while(oThisMenu.next_id!=0)
		{
			oThisMenu =  g_oMenuChain[oThisMenu.next_id];	// 链表后移
			
			if(oThisMenu.level<=oMenu.level)
			{
				oThisMenu = g_oMenuChain[oThisMenu.pre_id];	// 找到不是 iMenuID 子孙的节点， 因为该节点的 level 等于或小于 iMenuID 对应的节点
				break;
			}
		}
	}

	if(oThisMenu.next_id==0)
	{
		// 如果找到的节点是链表尾， 则新添加的节点为新的表尾
		oThisMenu.next_id = g_iNewMenuID;
		g_oMenuChain[g_iNewMenuID] = {"pre_id":oThisMenu.id,"next_id":0,"id":g_iNewMenuID,"name":"","parent_id":iMenuID,"level":oMenu.level+1};
	}
	else
	{
		//在链表中插入新节点，
		var oNextMenu =  g_oMenuChain[oThisMenu.next_id];
		oThisMenu.next_id = g_iNewMenuID;
		oNextMenu.pre_id = g_iNewMenuID;
		g_oMenuChain[g_iNewMenuID] = {"pre_id":oThisMenu.id,"next_id":oNextMenu.id,"id":g_iNewMenuID,"name":"","parent_id":iMenuID,"level":oMenu.level+1};
	}

	
	// 输出显示
	var str = '<tr id="row-'+g_iNewMenuID+'" onMouseOver="this.className=\'row-on\'" onMouseOut="this.className=\'row-off\'">'
	str += '<td></td>'
	str += '<td></td>'
	str += '<td>';
	for(var i=0; i<=oMenu.level; i++)
	{
		str += '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ';
	}
	str += '|_ ';
	str += '<input type="hidden" name="id[]" id="id-'+g_iNewMenuID+'" value="0" />';
	str += '<input type="hidden" name="parent_id[]" id="parent_id-'+g_iNewMenuID+'" value="'+iMenuID+'" />';
	str += '<input type="text" class="m_name" name="name[]" id="name-'+g_iNewMenuID+'" size="20" maxlength="120" />';
	str += '</td>';
	str += '<td align="center">';
	str += '<a href="javascript:;" onclick="javascript:setLink('+g_iNewMenuID+');" id="link-'+g_iNewMenuID+'" title="点击设置">未设置</a>';
	str += '<input type="hidden" class="m_controller" name="m_controller[]" id="controller-'+g_iNewMenuID+'" />';
	str += '<input type="hidden" name="m_task[]" id="task-'+g_iNewMenuID+'" />';
	str += '<input type="hidden" name="param[]" id="param-'+g_iNewMenuID+'" />';
	str += '<input type="hidden" name="link_to[]" id="link_to-'+g_iNewMenuID+'" />';
	str += '</td>';
	str += '<td align="center">';
	str += '<input type="text" name="url[]" id="url-'+g_iNewMenuID+'" size="20" maxlength="120" />';
	str += '</td>';
	str += '<td align="center">';
	str += '<a href="javascript:;" onclick="javascript:orderUp('+g_iNewMenuID+')" class="order-up-on" id="order-up-'+g_iNewMenuID+'"></a>';
	str += '</td>';
	str += '<td align="center">';
	str += '<a href="javascript:;" onclick="javascript:orderDown('+g_iNewMenuID+')" class="order-down-on" id="order-down-'+g_iNewMenuID+'"></a>';
	str += '</td>';
	str += '<td align="center"><a href="javascript:;" onclick="javascript:deleteMenu('+g_iNewMenuID+')" class="delete"></a></td>';
	str += '</tr>';

	$("#row-"+oThisMenu.id).after(str);
	

	var $e = $("#toggle-"+iMenuID);
	if(oMenu.children)
	{
		if($e.attr("class") == "toggle-off")	// 添加新节点时， 如果父节点是合上的， 则自动打开
			togleSubMenu(iMenuID)
	}
	else
	{
		$e.fadeIn();
	}
	
	// 更新孩子节点数
	oMenu.children++;
	
	updateOrderIcon();
}

// 打开/合上子节点
function togleSubMenu(iMenuID)
{
	var $e = $("#toggle-"+iMenuID);
	if($e.attr("class") == "toggle-on")
		$e.attr("class","toggle-off");
	else
		$e.attr("class","toggle-on");

	var oMenu = g_oMenuChain[iMenuID];
	var oThisMenu = oMenu;
	while(oThisMenu.next_id>0)
	{
		oThisMenu =  g_oMenuChain[oThisMenu.next_id];	// 链表后移
		if(oThisMenu.level>oMenu.level)
		{
			if($e.attr("class") == "toggle-on")
			{
				$("#row-"+oThisMenu.id).show();
				$("#toggle-"+oThisMenu.id).attr("class","toggle-on");
			}
			else
			{
				$("#row-"+oThisMenu.id).hide();
				$("#toggle-"+oThisMenu.id).attr("class","toggle-off");
			}
		}
		else
			break;	
	}
}


function updateOrderIcon()
{
	if(g_iMenuChainHead==0) return;

	var oMenu = g_oMenuChain[g_iMenuChainHead];
	var oThisMenu = oMenu;
	while(oThisMenu.next_id!=0)
	{
		if(oThisMenu.pre_id)
		{
			if( g_oMenuChain[oThisMenu.pre_id].level<oThisMenu.level)
				$("#order-up-"+oThisMenu.id).attr("class", "order-up-off");
			else
				$("#order-up-"+oThisMenu.id).attr("class", "order-up-on");
		}
		else
			$("#order-up-"+oThisMenu.id).attr("class", "order-up-off");

		if( g_oMenuChain[oThisMenu.next_id].level<oThisMenu.level)
			$("#order-down-"+oThisMenu.id).attr("class", "order-down-off");
		else
			$("#order-down-"+oThisMenu.id).attr("class", "order-down-on");

		oThisMenu = g_oMenuChain[oThisMenu.next_id];
	}
	
	if(oThisMenu.pre_id)
		$("#order-up-"+oThisMenu.id).attr("class", "order-up-on");
	else
		$("#order-up-"+oThisMenu.id).attr("class", "order-up-off");
	
	$("#order-down-"+oThisMenu.id).attr("class", "order-down-off");
	
	if(oThisMenu.level !=0)
	{
		while(oThisMenu.level !=0)
		{
			oThisMenu = g_oMenuChain[oThisMenu.pre_id];
		}
		$("#order-down-"+oThisMenu.id).attr("class", "order-down-off");
	}
}



// 上移菜单
function orderUp(iMenuID)
{
	if( $("#order-up-"+iMenuID).attr("class") == "order-up-off" ) return;
	
	var oMenu = g_oMenuChain[iMenuID];
	
	var oPreMenuHead = g_oMenuChain[oMenu.pre_id];
	var oPreMenuTail = g_oMenuChain[oMenu.pre_id];
	
	while(oPreMenuHead.pre_id!=0 && oPreMenuHead.level>oMenu.level) oPreMenuHead = g_oMenuChain[oPreMenuHead.pre_id];

	if(oPreMenuHead.pre_id==0)
		g_iMenuChainHead = oMenu.id;
	else
		g_oMenuChain[oPreMenuHead.pre_id].next_id = oMenu.id;

	oMenu.pre_id = oPreMenuHead.pre_id;

	$("#row-"+oPreMenuHead.id).before($("#row-"+oMenu.id));
	$("#row-"+oMenu.id).mouseout();
	
	
	var oLastMovedMenu;
	if(oMenu.next_id==0)
	{
		oLastMovedMenu = oMenu;
	}
	else
	{
		oLastMovedMenu = g_oMenuChain[oMenu.next_id];
		while(oLastMovedMenu.next_id!=0 && oLastMovedMenu.level>oMenu.level)
		{
			$("#row-"+oPreMenuHead.id).before($("#row-"+oLastMovedMenu.id));
			oLastMovedMenu =  g_oMenuChain[oLastMovedMenu.next_id];
		}
		
		if(oLastMovedMenu.next_id==0)
		{
			if(oLastMovedMenu.level>oMenu.level)
				$("#row-"+oPreMenuHead.id).before($("#row-"+oLastMovedMenu.id));
			else
				oLastMovedMenu = g_oMenuChain[oLastMovedMenu.pre_id];
		}
		else
			oLastMovedMenu = g_oMenuChain[oLastMovedMenu.pre_id];
	}
	
	if(oLastMovedMenu.next_id!=0)
		g_oMenuChain[oLastMovedMenu.next_id].pre_id = oPreMenuTail.id;
		
	oPreMenuHead.pre_id = oLastMovedMenu.id;
	oPreMenuTail.next_id = oLastMovedMenu.next_id;
	oLastMovedMenu.next_id = oPreMenuHead.id;

	updateOrderIcon();
}


function orderDown(iMenuID)
{
	if( $("#order-down-"+iMenuID).attr("class") == "order-down-off" )return;
	
	var oMenu = g_oMenuChain[iMenuID];

	var oNextMenuHead = g_oMenuChain[oMenu.next_id];
	while( oNextMenuHead.level>oMenu.level ) oNextMenuHead = g_oMenuChain[oNextMenuHead.next_id];

	var oNextMenuTail = oNextMenuHead;
	while(oNextMenuTail.next_id!=0 && g_oMenuChain[oNextMenuTail.next_id].level>oNextMenuHead.level) oNextMenuTail = g_oMenuChain[oNextMenuTail.next_id];

	if(oMenu.pre_id==0)
		g_iMenuChainHead = oNextMenuHead.id;
	else
		g_oMenuChain[oMenu.pre_id].next_id = oNextMenuHead.id;

	oNextMenuHead.pre_id = oMenu.pre_id;
	oMenu.pre_id = oNextMenuTail.id;
	
	oLastMovedMenu = oMenu;
	while(oLastMovedMenu.next_id != oNextMenuHead.id)
	{
		$("#row-"+oLastMovedMenu.pre_id).after($("#row-"+oLastMovedMenu.id));
		oLastMovedMenu = g_oMenuChain[oLastMovedMenu.next_id];
	}
	$("#row-"+oLastMovedMenu.pre_id).after($("#row-"+oLastMovedMenu.id));
	$("#row-"+oMenu.id).mouseout();
	
	if(oNextMenuTail.next_id!=0)
		g_oMenuChain[oNextMenuTail.next_id].pre_id = oLastMovedMenu.id;

	oLastMovedMenu.next_id = oNextMenuTail.next_id;
	oNextMenuTail.next_id = oMenu.id;

	updateOrderIcon();
}


function confirmDeleteMenu(iMenuID)
{
	// 如果是新添加的节点， 直接删除
	if($("#id-"+iMenuID).val() == 0)
	{
		deleteSingleMenu(iMenuID);
		return;
	}
	
	var oMenu = g_oMenuChain[iMenuID];	// 获取当前操作的节点
	
	var msg = '';
	if(oMenu.children)
		msg = '本操作将同时删除本菜单下的子菜单, 确认要删除吗?';
	else
		msg = '确认要删除当前菜单吗?';

	if( confirm(msg) ) deleteMenu(iMenuID)
}

// 删除菜单
function deleteMenu(iMenuID)
{
	var oMenu = g_oMenuChain[iMenuID];	// 获取当操作的节点
	var oThisMenu = oMenu;
	while(oThisMenu.next_id>0)
	{
		oThisMenu =  g_oMenuChain[oThisMenu.next_id];	// 链表后移
		
		if(oThisMenu.level>oMenu.level)
			deleteSingleMenu(oThisMenu.id);
		else
			break;	
	}
	deleteSingleMenu(iMenuID);
	
	updateOrderIcon();
}

function deleteSingleMenu(iMenuID)
{
	var oMenu = g_oMenuChain[iMenuID];	// 获取当前操作的节点

	// 如果有直接前趋, 则把当前节点的直接后继赋值给上一个节点的直接后继
	if(oMenu.pre_id)
		g_oMenuChain[oMenu.pre_id].next_id = oMenu.next_id;
	else
		g_iMenuChainHead = oMenu.next_id;
	
	// 如果有直接后继, 则把当前节点的直接前趋赋值给下一个节点的直接前趋
	if(oMenu.next_id)
		g_oMenuChain[oMenu.next_id].pre_id = oMenu.pre_id;

	
	if(oMenu.parent_id)
	{
		// 判数父节点是否已清空， 如果已为空， 则隐藏打开/缩起图标
		var oParentMenu = g_oMenuChain[oMenu.parent_id];
		if(oParentMenu)
		{
			if(oParentMenu.next_id)
			{
				if( g_oMenuChain[oParentMenu.next_id].level<= oParentMenu.level )
				{
					oParentMenu.children = 0;	//下一个节点比当前节点的级别低， 证明父节点已没有孩子节点, 设置父节点的孩子数为0
					$("#toggle-"+oParentMenu.id).fadeOut();	// 隐藏缩放图标
				}
			}
			else
			{
				oParentMenu.children = 0;	//父节点的直接后继为空， 为链表的结尾， 因些设置父节点的孩子数为0
				$("#toggle-"+oParentMenu.id).fadeOut();	// 隐藏缩放图标
			}
		}
	}
	
	g_oMenuChain[iMenuID] = null;
	
	
	if($("#id-"+iMenuID).val() == 0)
	{
		// 如果是新添加的菜单， 直接删队， 无须提交服务器端
		$("#row-"+iMenuID).remove();
		return;
	}

	// 提交服务器删除操作
	$.ajax({
		type: "GET",
		url: "./?app=menu&task=ajax_delete_menu&menu_id="+iMenuID,
		dataType: "json",
		success: function(json){
			if(json.status=="0")
			{
				$("#row-"+iMenuID).remove();
			}
			else
			{
				alert("删除失败: "+json.description);
			}		
		}
	});	
}




function setLink(iMenuID)
{
	var iWidth = 500;
	var iHeight = 300;
	var iBrowserWidth = $(window).width();
	var iBrowserHeight = $(window).height();
	var iLeft = (iBrowserWidth-iWidth)/2;
	var iTop = (iBrowserHeight-iHeight)/2;
		
	window.open("./?app=menu&task=ajax_link&menu_id="+iMenuID+"&is_backend="+g_iIsBackend, "ajax_link", "width="+iWidth+", height="+iHeight+", left="+iLeft+", top="+iTop+", toolbar=no, menubar=no, location=no, status=no, scrollbars=yes");
}

function saveLink(iMenuID, sController, sTask, sParam, sLink)
{
	$("#link-"+iMenuID).html(sLink);
	
	$("#controller-"+iMenuID).val(sController);
	$("#task-"+iMenuID).val(sTask);
	$("#param-"+iMenuID).val(sParam);
	$("#link_to-"+iMenuID).val(sLink);	
}



function chkForm()
{
	var bResult = true;
	$(".m_name").each(function(i){
		if($.trim($(this).val())=="") 
		{
			if(bResult)  alert("菜单名不能为空");
			bResult = false;
		}
	});
	
	if(!bResult) return bResult;

	$(".m_controller").each(function(i){
		if($.trim($(this).val())=="") 
		{
			if(bResult) alert("菜单必须设置连接到应用");
			bResult = false;
		}
	});

	return bResult;
}


















